package com.tdam2014.grupo3.db;

import java.sql.Date;
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.tdam2014.grupo3.entities.Photo;

public class PhotoDatabaseHelper extends DatabaseHelper {

	public PhotoDatabaseHelper(Context context) {
		super(context);
	}
	
	public ArrayList<Photo> getPhotoList(String photoSetID, String order) {
		SQLiteDatabase db = this.getReadableDatabase();
		
		String query = "SELECT * FROM " + tblPhoto + " WHERE " + colPhotoPhotoSetID + "=? ORDER BY " + order;
		Cursor cursor = db.rawQuery(query, new String [] {photoSetID});
		
		Photo photo;
		ArrayList<Photo> photos = new ArrayList<Photo>();
		while (cursor.moveToNext()) {
			photo = new Photo();
			photo.setId(cursor.getString(cursor.getColumnIndex(colPhotoID)));
			photo.setTitle(cursor.getString(cursor.getColumnIndex(colPhotoTitle)));
			photo.setDatePosted(new Date(cursor.getLong(cursor.getColumnIndex(colPhotoPosted))));
			photo.setFarm(cursor.getInt(cursor.getColumnIndex(colPhotoFarm)));
			photo.setSecret(cursor.getString(cursor.getColumnIndex(colPhotoSecret)));
			photo.setServer(cursor.getString(cursor.getColumnIndex(colPhotoServer)));
			photo.setCountComments(cursor.getInt(cursor.getColumnIndex(colPhotoCountComments)));
			
			photos.add(photo);
		}
		
		return photos;
	}
	
	public void addPhotos(ArrayList<Photo> photos, String photoSetID) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.beginTransaction();
		
		ContentValues cv = new ContentValues();
		for(Photo photo : photos) {
			cv.put(colPhotoPhotoSetID, photoSetID);
			cv.put(colPhotoTitle, photo.getTitle());
			cv.put(colPhotoPosted, photo.getDatePosted().getTime());
			cv.put(colPhotoFarm, photo.getFarm());
			cv.put(colPhotoSecret, photo.getSecret());
			cv.put(colPhotoServer, photo.getServer());
			cv.put(colPhotoCountComments, photo.getCountComments());
			
			if(existPhoto(photo.getId())) {
				db.update(tblPhoto, cv, colPhotoID + "=?", new String[] {photo.getId()});
			} else {
				cv.put(colPhotoID, photo.getId());
				db.insert(tblPhoto, null, cv);
			}
		}
		
		db.setTransactionSuccessful();
		db.endTransaction();
		db.close();
	}
	
	private boolean existPhoto(String id) {
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(tblPhoto, null, colPhotoID + "=?", new String[] {id}, null, null, null);
		return cursor.moveToFirst();
	}
	
	public int getPhotoCountComments(String id) {
		SQLiteDatabase db = this.getReadableDatabase();
		
		Cursor cursor = db.query(tblPhoto, new String[] {colPhotoCountComments}, colPhotoID + "=?", new String[] {id}, null, null, null);
		if(cursor.moveToFirst()) return cursor.getInt(cursor.getColumnIndex(colPhotoCountComments));
		else return -1;
	}
	
	public void updatePhotoCountComments(String id, int countComments) {
		SQLiteDatabase db = this.getWritableDatabase();
		
		ContentValues cv = new ContentValues();
		cv.put(colPhotoCountComments, countComments);
		db.update(tblPhoto, cv, colPhotoID + "=?", new String[] {id});
		
		db.close();
	}
}
